<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <!-- 组件整合，其他组件需要定义在这个div内部 -->
    <SearchForm
      :searchData="pagination"
      @handleSearch="handleSearch"
      @handleReset="handleReset"
    ></SearchForm>

    <TableList
      :listData="listData"
      :total="total"
      :pagination="pagination"
      @getCurrent="getCurrent"
      @handleBulid="handleBulid"
      @handleEdit="handleEdit"
      @handleForbidden="handleForbidden"
      @handleClickDelete="handleClickDelete"
    ></TableList>

    <DialogFrom
      ref="formRef"
      :visible="visible"
      :planData="planData"
      :data="formBaseData"
      :title="title"
      @handleClose="handleClose"
      @handleAdd="handleAdd"
      @handleEditForm="handleEditForm"
    ></DialogFrom>

    <Delete
      :visible="dialogDeleteVisible"
      :delete-text="operateText"
      @handle-delete="handleDelete"
      @handle-close="handleDeleteClose"
    ></Delete>

    <Forbidden
      :visible="dialogVisible"
      :text="operateText"
      @handle-submit="handleForbiddenSub"
      @handle-close="handleForbiddenClose"
    ></Forbidden>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getLevelList, getnursAllPlanList, getLevelDetails, levelUpdate, levelAdd, levelDelete, levelStatus } from "@/api/serve";
import TableList from "./components/TableList.vue";
import SearchForm from "./components/SearchForm.vue";
import DialogFrom from "./components/DialogFrom.vue";
import { MessagePlugin } from 'tdesign-vue-next';
import Delete from '@/components/OperateDialog/index.vue'
import Forbidden from '@/components/Forbidden/index.vue'

// 列表数据
const listData = ref([]);
// 新增，编辑弹窗
const visible = ref(false);
// 全部护理等级数据
const planData = ref([]);
// 弹窗标题
const title = ref('');
// 弹窗表单内容
const formBaseData = ref({});
const formRef = ref(null)
// 删除弹窗显示
const dialogDeleteVisible = ref(false);
// 设置删除id
const typeId = ref('')
// 要操作的内容提示
const operateText = ref('护理等级') 
// 分页
const pagination = ref({
  pageSize: 10,
  pageNum: 1 // 默认当前页
})
// 总数
const total = ref(0)
// 禁用启用
const typeStatus = ref(null)
// 禁用弹窗
const dialogVisible = ref(false)
// 启用禁用提示
const statusText = ref('')



// 查询列表数据
const getList = async () => {
  const result: any = await getLevelList(pagination.value);
  listData.value = result.data.records;
  total.value = result.data.total;
}

// 获取全部护理计划
const getAll = async () => {
  const result: any = await getnursAllPlanList();
  planData.value = result.data
}

// 根据id获取详情数据
const getDetails = async (id) => {
  const result: any = await getLevelDetails(id);
  formBaseData.value = result.data;
}

// 添加
const handleAdd = async (val) => {
  const res: any = await levelAdd(val)
  if (res.code === 200) {
    MessagePlugin.success('添加成功')
    getList()
    handleClose()
    formRef.value.handleClear()
  }
}

// 编辑
const handleEditForm = async (val) => {
  const res: any = await levelUpdate(val)
  if (res.code === 200) {
    MessagePlugin.success('编辑成功')
    getList()
    handleClose()
    formRef.value.handleClear()
  }
}

// 点击删除
const handleClickDelete = (val) => {
  typeId.value = val.id
  dialogDeleteVisible.value = true
}

// 确认删除
const handleDelete = async () => {
  const res: any = await levelDelete(typeId.value)
  if (res.code === 200) {
    dialogDeleteVisible.value = false
    MessagePlugin.success('删除成功')
    getList()
  }
}

// 确定禁用
const handleForbiddenSub = async () => {
  const params = {
    id: typeId.value,
    status: typeStatus.value
  }
  const res: any = await levelStatus(params)
  if (res.code === 200) {
    dialogVisible.value = false
    MessagePlugin.success(statusText.value)
    getList()
  }
}

// 关闭禁用弹窗
const handleForbiddenClose = () => {
  dialogVisible.value = false
}

// 关闭删除弹层
const handleDeleteClose = () => {
  dialogDeleteVisible.value = false
}

// 禁用弹窗
const handleForbidden = (val) => {
  typeId.value = val.id
  if (val.status === 1) {
    dialogVisible.value = true
    typeStatus.value = 0
    statusText.value = '禁用成功'
  } else {
    typeStatus.value = 1
    handleForbiddenSub()
    statusText.value = '启用成功'
  }
}

// 翻页设置当前页
const getCurrent = (val) => {
  pagination.value.pageNum = val.current
  pagination.value.pageSize = val.pageSize
  getList()
}

// 搜索
const handleSearch = () => {
  pagination.value.pageNum = 1
  getList()
}

// 重置，清空搜索框
const handleReset = () => {
  // 重置页码
  pagination.value = {
    pageSize: 10,
    pageNum: 1
  }
  getList()
}

// 开启弹窗
const handleBulid = () => {
  visible.value = true;
  title.value = '新增'
}

// 关闭弹窗
const handleClose = () => {
  visible.value = false;
}

// 编辑
const handleEdit = (val) => {
  // 获取详情

  getDetails(val.id)
  // 显示弹窗
  visible.value = true
  // 将弹窗的标题
  title.value = '编辑'
}

onMounted(() => {
  getList();
  getAll();
})

</script>